package com.eureka.eurekaserviceinvoker.filter;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

/**
 *  创建与销毁Hystrix请求上下文，必须使用@ServletComponentScan注解扫描
 *  这里只针对每次请求缓存，每次请求后结束后，缓存就会消失
 * Created by earl on 2018/5/2.
 */
@WebFilter(urlPatterns = "/*",filterName = "hystrixFilter")
public class HystrixFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        }finally {
            context.shutdown();
        }
    }

    @Override
    public void destroy() {

    }
}
